package com.gy.smartmanagement.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import butterknife.BindView;
import okhttp3.RequestBody;

import com.google.gson.Gson;
import com.gy.smartmanagement.R;
import com.gy.smartmanagement.base.api.bean.ErrorResultBean;
import com.gy.smartmanagement.base.app.AppApplication;
import com.gy.smartmanagement.base.fragment.BaseFragment;

import com.gy.smartmanagement.home.MainActivity;
import com.gy.smartmanagement.login.mvp.LoginBean;
import com.gy.smartmanagement.login.mvp.LoginBody;
import com.gy.smartmanagement.login.mvp.LoginContract;
import com.gy.smartmanagement.login.mvp.WeatherBean;
import com.gy.smartmanagement.utils.ConstanceValue;


/**
 * Created by MVPModule on 11/15/2019 11:55
 */
public class LoginFragment extends BaseFragment implements LoginContract.IView {

  public static final String TAG = "LoginFragment";
  private LoginContract.Presenter mPresenter;

  @BindView(R.id.name_et)
  EditText name_et;
  @BindView(R.id.pwd_et)
  EditText pwd_et;
  @BindView(R.id.code_et)
  EditText code_et;
  @BindView(R.id.login_tv)
  TextView login_tv;

  public static LoginFragment newInstance() {
    Bundle args = new Bundle();

    LoginFragment fragment = new LoginFragment();
    fragment.setArguments(args);
    return fragment;
  }

  @Override
  public int getLayoutResId() {
    return R.layout.fragment_login;
  }

  @Override
  public void initialization() {
    login_tv.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        String name = name_et.getText().toString();
        String pwd = pwd_et.getText().toString();
        String code = code_et.getText().toString();
        if (!name.equals("") && !pwd.equals("") && !code.equals("")) {
          LoginBody loginBody=new LoginBody();
          loginBody.setAccount(name);
          loginBody.setPassword(pwd);
          loginBody.setCaptcha(code);
          String str=new Gson().toJson(loginBody);
          RequestBody body= RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),str);
          mPresenter.login(body);
        } else {
          showToastTip("请输入完整信息!");
        }
      }
    });
  }


  @Override
  public void setPresenter(@NonNull LoginContract.Presenter presenter) {
    mPresenter = presenter;
  }

  @Override
  public void onLoadFail(ErrorResultBean.ErrorBean errorBean) {
    showToastTip(errorBean.getErrorResult());
  }

  @Override
  public void show() {
    showLoadingWithStatus(childManager, "获取数据中...");
  }

  @Override
  public void hide() {
    hideLoading();
  }

  @Override
  public void onUnBind() {
    mPresenter.unBindPresent();
  }

  @Override
  public void loginSuccess(LoginBean loginBean) {
    AppApplication.getInstance().saveCacheData(ConstanceValue.TOKEN, loginBean.getToken());
    startActivity(new Intent(getContext(), MainActivity.class));
    getActivity().finish();
  }

}
